package com.example.note;

import java.util.ArrayList;
import java.util.List;

import com.example.note.MainActivity2;
import com.example.note.MainActivity3;

//import android.location.Address;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class MainActivity extends Activity {

	private Button addButton;
	private ListView lvTodos;
	
	//private Notatka NOTATKA;
	private NotatkaDB notatkaDB;
	private Cursor todoCursor;
	private List<Notatka> note;
	private NotatkaAdapter listAdapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		addButton=(Button)findViewById(R.id.btnNew);
		lvTodos=(ListView)findViewById(R.id.lvTodos);		
		this.initListView();
		this.addListenerOnButton();
		this.initListViewOnItemClick();
	}
	/*****/
	@Override
	protected void onResume() {
		  super.onResume();
		  this.initListView();
	}
	
	/*****/
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	/*****/
	private void addListenerOnButton(){
		final Context kontekst = this;
		addButton.setOnClickListener(new OnClickListener() {
			@Override
 			public void onClick(View widok) {
 				Intent intencja= new Intent(kontekst,MainActivity2.class);
 				startActivity(intencja);
 			}
     	 });
	}
	/*****/
	private void initListView() {
		fillListViewData();
		//initListViewOnItemClick();
	}
	/*****/
	private void fillListViewData() {
		notatkaDB = new NotatkaDB(getApplicationContext());
		notatkaDB.open();
		getAllTasks();
		listAdapter = new NotatkaAdapter(this, note);
		lvTodos.setAdapter(listAdapter);
	}
	/*****/
	private void getAllTasks() {
		note = new ArrayList<Notatka>();
		todoCursor = getAllEntriesFromDb();
		updateTaskList();
	}
	/*****/
	private Cursor getAllEntriesFromDb() {
		todoCursor = notatkaDB.getAllNote();
		if(todoCursor != null) {
			startManagingCursor(todoCursor);
			todoCursor.moveToFirst();
		}
		return todoCursor;
	}
	/*****/
	private void updateTaskList() {
		if(todoCursor != null && todoCursor.moveToFirst()) {
			do {
				long id = todoCursor.getLong(NotatkaDB.ID_COLUMN);
				String tresc = todoCursor.getString(NotatkaDB.TRESC_COLUMN);
				int priorytet = todoCursor.getInt(NotatkaDB.PRIORYTET_COLUMN);
				boolean completed = todoCursor.getInt(NotatkaDB.ZAKONCZONY_COLUMN) > 0 ? true : false;
				note.add(new Notatka(id, tresc,priorytet, completed));
			} while(todoCursor.moveToNext());
		}
	}
	/*****/
	private void initListViewOnItemClick() {
		final Context kontekst = this;
		lvTodos.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View v, int position,
					long id) {
				Notatka notatka = note.get(position);
				Intent intent = new Intent(kontekst,MainActivity3.class);
				
				Bundle bundle = new Bundle();
				bundle.putLong("idNotatka", notatka.getId());
				bundle.putString("trescNotatka", notatka.getTresc());
				bundle.putInt("prioNotatka", notatka.getPriorytet());
				
				intent.putExtras(bundle);
				startActivity(intent);
				}
		});
	}
}
